package com.example.mall.web.controller;

import com.example.mall.domain.TimeLimitedDiscounts;
import com.example.mall.dto.TimeLimitedDiscountsDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.PageDTO;
import com.example.mall.query.TimeLimitedDiscountsQuery;
import com.example.mall.result.JSONResult;
import com.example.mall.service.ITimeLimitedDiscountsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/timeLimitedDiscounts")
public class TimeLimitedDiscountsController {

    @Autowired
    public ITimeLimitedDiscountsService timeLimitedDiscountsService;

    /**
     * 分页查询限时折扣表信息
     * @param timeLimitedDiscountsQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询限时折扣表信息",notes = "分页查询限时折扣表信息",httpMethod = "GET")
    public JSONResult queryPage(TimeLimitedDiscountsQuery timeLimitedDiscountsQuery){
        PageDTO<TimeLimitedDiscountsDto> timeLimitedDiscountsDtoPageDTO = timeLimitedDiscountsService.queryPage(timeLimitedDiscountsQuery);
        return JSONResult.success("查询成功",timeLimitedDiscountsDtoPageDTO);
    }

    /**
     * 根据id查询限时折扣表信息
     * @param discountId
     * @return
     */
    @GetMapping("query/{discountId}")
    @ApiOperation(value = "根据id查询限时折扣表信息",notes = "根据id查询限时折扣表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("discountId")Long discountId){
        //1.根据id查询用户信息
        if (discountId == null){
            throw new GlobalCustomerException(ErrorEnumCode.DISCOUNTID_NOT_ID);
        }
        TimeLimitedDiscounts timeLimitedDiscounts = timeLimitedDiscountsService.getById(discountId);
        return JSONResult.success("查询成功",timeLimitedDiscounts);
    }

    /**
     * 根据id查询限时折扣表信息
     * @param discountId
     * @return
     */
    @GetMapping("queryTimeLimitedDiscountsById/{discountId}")
    @ApiOperation(value = "根据id查询限时折扣表信息",notes = "根据id查询限时折扣表信息",httpMethod = "GET")
    public JSONResult queryTimeLimitedDiscountsById(@PathVariable("discountId")Long discountId){
        //1.根据id查询用户信息
        if (discountId == null){
            throw new GlobalCustomerException(ErrorEnumCode.DISCOUNTID_NOT_ID);
        }
        TimeLimitedDiscounts timeLimitedDiscounts = timeLimitedDiscountsService.queryTimeLimitedDiscountsById(discountId);
        return JSONResult.success("查询成功",timeLimitedDiscounts);
    }

    /**
     * 查询全部限时折扣表信息
     * @param
     * @return
     */
    @GetMapping("queryTimeLimitedDiscountsList")
    @ApiOperation(value = "查询全部限时折扣表信息",notes = "查询全部限时折扣表信息",httpMethod = "GET")
    public JSONResult queryTimeLimitedDiscountsList(){
        List<TimeLimitedDiscounts> timeLimitedDiscounts = timeLimitedDiscountsService.queryTimeLimitedDiscountsList();
        return JSONResult.success("查询成功",timeLimitedDiscounts);
    }

    /**
     * 新增限时折扣表
     * @param timeLimitedDiscountsDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增限时折扣表",notes = "新增限时折扣表",httpMethod = "POST")
    public JSONResult insert(@RequestBody TimeLimitedDiscountsDto timeLimitedDiscountsDto){
        timeLimitedDiscountsService.insert(timeLimitedDiscountsDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改限时折扣表信息
     * @param timeLimitedDiscountsDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改限时折扣表信息",notes = "修改限时折扣表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody TimeLimitedDiscountsDto timeLimitedDiscountsDto){
        timeLimitedDiscountsService.update(timeLimitedDiscountsDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除限时折扣表信息
     * @param discountId
     * @return
     */
    @PostMapping("delete/{discountId}")
    @ApiOperation(value = "删除限时折扣表信息",notes = "删除限时折扣表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("discountId") Long discountId) {
        timeLimitedDiscountsService.delete(discountId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除限时折扣表信息
     * @param discountIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除限时折扣表信息",notes = "批量删除限时折扣表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> discountIds) {
        timeLimitedDiscountsService.deleteBatch(discountIds);
        return JSONResult.success("批量删除成功");
    }
}
